﻿using GalaSoft.MvvmLight;
using MyToolkit.Multimedia;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Youtube_HD.Model;

namespace Youtube_HD.Services
{
    public class HomeViewModel : ViewModelBase, IHomeViewModel
    {
        #region Static
        public static string Feature = "featured";
        public static string Popular = "most_popular";
        public static string Recent = "most_recent";
        public static string Trending = "trending";
        #endregion
        IYoutubeServices youtubeServices;
        bool loadingCompalete = false;
        public bool LoadingComplete
        {
            get
            {
                return loadingCompalete;
            }
            set
            {
                loadingCompalete = value;
                RaisePropertyChanged(() => LoadingComplete);
            }
        }
        bool searchComplete = false;
        public bool SearchComplete
        {
            get
            {
                return searchComplete;
            }
            set
            {
                searchComplete = value;
                RaisePropertyChanged(() => SearchComplete);
            }
        }
        //Choose the gap of the videos list Index
        int index = 1;
        //The number of videos you would like to get (from 1 to 50)
        int max_results = 50;

        //To get more than 50 videos, just use pagination and change the index :
        //int index = 51;
        ObservableCollection<YoutubeVideo> youtubeVideos;
        public ObservableCollection<YoutubeVideo> YoutubeVideos
        {
            get
            {
                if (youtubeVideos == null)
                    youtubeVideos = new ObservableCollection<YoutubeVideo>();
                return youtubeVideos;
            }
            set
            {
                youtubeVideos = value;
                RaisePropertyChanged(() => YoutubeVideos);
            }
        }
        ObservableCollection<YoutubeVideo> youtubeVideosSearch;
        public ObservableCollection<YoutubeVideo> YoutubeVideosSearch
        {
            get
            {
                if (youtubeVideosSearch == null)
                    youtubeVideosSearch = new ObservableCollection<YoutubeVideo>();
                return youtubeVideosSearch;
            }
            set
            {
                youtubeVideosSearch = value;
                RaisePropertyChanged(() => YoutubeVideosSearch);
            }
        }
        ObservableCollection<YoutubeVideo> youtubeVideosTrailers;
        public ObservableCollection<YoutubeVideo> YoutubeVideosTrailers
        {
            get
            {
                if (youtubeVideosTrailers == null)
                    youtubeVideosTrailers = new ObservableCollection<YoutubeVideo>();
                return youtubeVideosTrailers;
            }
            set
            {
                youtubeVideosTrailers = value;
                RaisePropertyChanged(() => YoutubeVideosTrailers);
            }
        }
        public HomeViewModel(IYoutubeServices youtubeServices)
        {
            this.youtubeServices = youtubeServices;
            GetYoutubeVideo("40", "most_popular");
            GetTrailersAsync();
        }
        public async Task<YouTubeUri> GetVideoUriAsync(string youTubeId, YouTubeQuality maxQuality)
        {
            return await youtubeServices.GetVideoUriAsync(youTubeId, maxQuality);
        }
        public async void GetYoutubeVideo(string max, string orderType)
        {
            LoadingComplete = true;
            string parm = string.Format("https://gdata.youtube.com/feeds/api/standardfeeds/{0}?time=today&max-results={1}&v=2",orderType, max);
            var channelVideos = await youtubeServices.GetYoutubeChannel(parm);
            if (channelVideos != null)
                YoutubeVideos = new ObservableCollection<YoutubeVideo>(channelVideos);
            LoadingComplete = false;
        }
        public async void GetSearchAsync(string url)
        {
            SearchComplete = true;
            var getData= await youtubeServices.GetYoutubeSearch(url);
            YoutubeVideosSearch = new ObservableCollection<YoutubeVideo>(getData);
            SearchComplete = false;
        }
        public async Task GetTrailersAsync()
        {
            SearchComplete = true;
            string url = "https://gdata.youtube.com/feeds/api/users/trailers/uploads?v=2&order=releasedate";
            if (YoutubeVideosTrailers != null && YoutubeVideosTrailers.Count>0)
            {
                url = YoutubeVideosTrailers[YoutubeVideosTrailers.Count-1].NextPage;
                url = url + "&order=releasedate";
            }
            var getData = await youtubeServices.GetYoutubeTrailler(url);
            if (getData!=null)
            {
                foreach(var m in getData)
                {
                    YoutubeVideosTrailers.Add(m);
                }
            }
            SearchComplete = false;
        }
    }
}
